const router = require('koa-router')()
const koaBody = require('koa-body')();
const request = require('superagent'); //不要忘记npm install
const co = require('co');
var config=require('../config/app.config');
module.exports = router.get('/inviteGetWxCode/:id',co.wrap(function*(ctx) {
    /**
     * 被邀请用户的微信授权
     * 微信授权登陆完成重定向 
     * 1.获取授权code
     * 2、带上当前商户或平台的编号和邀请人id请求后台
     * 3、获取到微信授权用户信息重定向到首页进行缓存
     */
    console.log(ctx.query.code)
    console.log(ctx.params.id)
    var res = yield request
    .post(config.host+'/api/crm/customer/invited')
    .send({ customerId: '0', orgId: config.orgId, merchantId: config.merchantId, code: ctx.query.code, inviter: ctx.params.id })
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json');
    console.log('新生成的用户id');
    console.log(res.body.data.customerId);
    let id = res.body.data.customerId;
    ctx.response.redirect('http://qingwei.flyray.me/#/home?customerId='+id)
  })).post('/createInviteQrCode', koaBody, co.wrap(function*(ctx) {
    console.log(ctx.request.body);
    // 调用远程接口获取用户授权信息
    let reqJson= ctx.request.body;
    reqJson['orgId']=config.orgId;
    reqJson['merchantId']=config.merchantId;
    var res = yield request
    .post(config.host+'/api/crm/customer/createInviteQrCode')
    .send(reqJson)
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json');
    ctx.response.body = res.body;
    //ctx.render('index', { title: 'Nunjucks', content: 'Feifeiyu yeah!' });
  })).post('/customer/query', koaBody, co.wrap(function*(ctx) {
    //客户信息查询
    console.log(ctx.request.body);
    let reqJson= ctx.request.body;
    reqJson['orgId']=config.orgId;
    reqJson['merchantId']=config.merchantId;
    var res = yield request
    .post(config.host+'/api/crm/customer/query')
    .send(reqJson)
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json');
    ctx.response.body = res.body;
    //ctx.render('index', { title: 'Nunjucks', content: 'Feifeiyu yeah!' });
  }));
